package org.orbis.spikezolt;

import java.util.Iterator;
import java.util.Random;

import javax.microedition.khronos.opengles.GL10;

import org.anddev.andengine.engine.Engine;
import org.anddev.andengine.entity.primitive.Rectangle;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.sprite.AnimatedSprite;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.entity.text.Text;
import org.anddev.andengine.opengl.font.Font;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.anddev.andengine.opengl.texture.region.TextureRegion;
import org.anddev.andengine.opengl.texture.region.TiledTextureRegion;
import org.anddev.andengine.ui.activity.BaseGameActivity;
import org.anddev.andengine.util.HorizontalAlign;

import android.graphics.Color;
import android.graphics.Typeface;
import android.util.Log;

public class Switch extends Updater {
	public AnimatedSprite spr; // Switch

	String type;

	public float alpha = 0.0f;

	public boolean active = false;

	SpikeZoltActivity sa;

	private TiledTextureRegion r;

	Switch(SpikeZoltActivity sa, int x, int y, String type) {
		this.sa = sa;
		this.type = type;

		r = sa.texManager.TR_redSwitch;
		if (type.equals("bSwitch")) {
			r = sa.texManager.TR_blueSwitch;
		} else if (type.equals("pSwitch")) {
			r = sa.texManager.TR_purpleSwitch;
		}

		spr = new AnimatedSprite(x, y + 3, r);
		spr.setCurrentTileIndex(0);
		sa.scene.attachChild(spr);
		spr.setZIndex(sa.OTHER_DEPTH);
		sa.scene.sortChildren();
	}

	@Override
	public void step() {
		if (!active) {
			if (spr.collidesWith(sa.pl.getSprite())) {
				if (!active) {
					active = true;
					spr.setCurrentTileIndex(1);
					sa.sndPlayer.sound_play(sa.sndPlayer.snd_switch);
					//sa.mapLoader.unload(sa.mapLoader.currentMap);
					Iterator<Block> it = sa.blocks.iterator();
					while (it.hasNext()) {
						Block b = it.next();
						if (type.equals("rSwitch") && b.type.equals("rBlock")) {
							b.destroy(true); it.remove();
						} else if (type.equals("bSwitch")
								&& b.type.equals("bBlock")) {
							b.destroy(true); it.remove();
						} else if (type.equals("pSwitch")
								&& b.type.equals("pBlock")) {
							b.destroy(true); it.remove();
						}
					}
				}
			}
		}
	}

	@Override
	public void destroy() {
		// TODO Auto-generated method stub
		//sa.scene.detachChild(spr);	
		spr.detachSelf();
	}
}
